﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Threading;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using BlogManager.Core.Security;

namespace BlogManager
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Login01_Authenticate(object sender, AuthenticateEventArgs e)
        {
            if (authenticate(Login01.UserName, Login01.Password))
            {
                e.Authenticated = true;
                Response.Redirect("~/Application/Default.aspx");
            }
            else
            {
                e.Authenticated = false;
            }
        }

        protected bool authenticate(string email, string password)
        {
            var user = Data.Entities.User.SingleOrDefault(x => x.Email == email && x.Password == password);
            
            if (user != null)
            {
                var authService = new AuthenticationService();
                authService.RegisterUser(user);

                return true;
            }

            return false;
        }

        
    }
}